<template>
  <div v-if="patient" class="container">
    <div class="sidebar">
      <div class="timeline">
        <div class="timeline-item active">
          <div class="timeline-dot red"></div>
          <div class="timeline-date">随访日期：{{patient.follow_up_records[0].follow_up_date}}</div>
          <div>{{patient.follow_up_records[0].follow_up_summary}}</div>
        </div>
      </div>
    </div>

    <div class="main-content">
      <div class="nav-tabs">
        <div class="nav-tab active">随访记录</div>
      </div>

      <div class="chat-container">
        <div class="audio-player">
          <div class="audio-player">
            <div @click="togglePlay" class="play-button">
              <img v-if="!isPlaying" src="@/assets/home-img/play.png">
              <img v-else src="@/assets/home-img/stop.png">
            </div>
            <div style="flex: 1;display: flex;align-items: center;">
              <input
                style="flex: 1;margin-right: 12px"
                type="range"
                min="0"
                :max="duration"
                v-model="currentTime"
                @input="seek"
              >
              <span>{{ formatTime(currentTime) }} / {{ formatTime(duration) }}</span>
            </div>
          </div>
        </div>

        <div v-for="item in (categorizeConversationBySpeaker(patient.follow_up_records[0].conversation)).aiResponses" class="message left">
          <div class="message-time">{{item.time}}</div>
          <div class="message-content">
            <div class="avatar robot">
              👨‍⚕️
            </div>
            <div class="bubble">
              {{item.content}}
            </div>
          </div>
          <div class="clear-both"></div>
        </div>

        <div v-for="item in (categorizeConversationBySpeaker(patient.follow_up_records[0].conversation)).patientResponses" class="message right">
          <div class="message-time">{{ item.time }}</div>
          <div class="message-content">
            <div class="avatar">
              👤
            </div>
            <div class="bubble">
              {{item.content}}
            </div>
          </div>
          <div class="message-footer">
          </div>
          <div class="clear-both"></div>
        </div>
      </div>

      <div class="chat-footer">
        <div class="input-area">

        </div>
        <div class="chat-footer-tools">
          <div class="tool-button">🎙️</div>
          <div class="tool-button send">▶</div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import {onMounted, ref} from 'vue';
import audioFile from '@/assets/home-img/source.mp3';
import {getPatientById} from '@/demo-data/data/followup.js';
import {useRouter, useRoute} from 'vue-router'

const router = useRouter()  // 用于编程式导航
const route = useRoute()    // 用于获取当前路由信息
const audio = ref(null);
const isPlaying = ref(false);
const duration = ref(0);
const currentTime = ref(0);
const volume = ref(0.7);
const updateInterval = ref(null);

const patient = ref()

onMounted(() => {
  audio.value = new Audio(audioFile);
  audio.value.volume = volume.value;

  audio.value.addEventListener('loadedmetadata', () => {
    duration.value = audio.value.duration;
  });

  audio.value.addEventListener('ended', () => {
    isPlaying.value = false;
    clearInterval(updateInterval.value);
  });

  //根据路由传入的患者id查询某个患者的本页数据
  if (route.query.patientId) {
    patient.value = getPatientById(route.query.patientId);
    console.log(patient.value, "patient.value")
  }
});

onBeforeUnmount(() => {
  if (audio.value) {
    audio.value.pause();
    clearInterval(updateInterval.value);
  }
});

const togglePlay = () => {
  if (isPlaying.value) {
    audio.value.pause();
    clearInterval(updateInterval.value);
  } else {
    audio.value.play();
    updateInterval.value = setInterval(() => {
      currentTime.value = audio.value.currentTime;
    }, 1000);
  }
  isPlaying.value = !isPlaying.value;
};

const seek = () => {
  audio.value.currentTime = currentTime.value;
};

const changeVolume = () => {
  audio.value.volume = volume.value;
};

const formatTime = (seconds) => {
  const mins = Math.floor(seconds / 60);
  const secs = Math.floor(seconds % 60);
  return `${mins.toString().padStart(2, '0')}:${secs.toString().padStart(2, '0')}`;
};

function categorizeConversationBySpeaker(conversation) {
  return {
    patientResponses: conversation.filter(item => item.speaker === "患者"),
    aiResponses: conversation.filter(item => item.speaker === "AI助手")
  };
}
</script>

<style scoped>
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  font-family: "PingFang SC", "Microsoft YaHei", sans-serif;
}

body {
  background-color: #f5f7fa;
  height: 100vh;
  display: flex;
}

.container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 20px;
  display: flex;
}

.sidebar {
  width: 280px;
  background-color: #fff;
  border-right: 1px solid #eaeaea;
  position: relative;
  overflow: hidden;
}

.add-button {
  position: absolute;
  top: 10px;
  right: 10px;
  background-color: #1890ff;
  color: white;
  border-radius: 50%;
  width: 24px;
  height: 24px;
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 16px;
  cursor: pointer;
  box-shadow: 0 2px 5px rgba(0, 0, 0, 0.2);
}

.timeline {
  padding: 15px 0;
  position: relative;
}


.timeline-item {
  position: relative;
  padding: 10px 20px 10px 40px;
  cursor: pointer;
}

.timeline-item.active {
  background-color: #e6f7ff;
}

.timeline-dot {
  position: absolute;
  width: 10px;
  height: 10px;
  border-radius: 50%;
  left: 15px;
  top: 16px;
  z-index: 1;
}

.timeline-dot.red {
  background-color: #f5222d;
}

.timeline-dot.green {
  background-color: #52c41a;
}

.timeline-dot.blue {
  background-color: #1890ff;
}

.timeline-date {
  color: #888;
  font-size: 14px;
}

.main-content {
  flex: 1;
  display: flex;
  flex-direction: column;
  background-color: #f5f7fa;
}

.nav-tabs {
  display: flex;
  border-bottom: 1px solid #eaeaea;
  background-color: #fff;
}

.nav-tab {
  padding: 12px 24px;
  cursor: pointer;
  position: relative;
  color: #666;
}

.nav-tab.active {
  color: #1890ff;
  font-weight: 500;
}

.nav-tab.active::after {
  content: "";
  position: absolute;
  bottom: 0;
  left: 24px;
  right: 24px;
  height: 2px;
  background-color: #1890ff;
}

.chat-container {
  flex: 1;
  padding: 20px;
  overflow-y: auto;
}

.audio-player {
  background-color: #fff;
  padding: 15px 20px;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
  margin-bottom: 20px;
  display: flex;
  align-items: center;
  width: 100%;
}

.audio-controls {
  display: flex;
  align-items: center;
  margin-bottom: 10px;
}

.play-button {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  display: flex;
  justify-content: center;
  align-items: center;
  margin-right: 15px;
  cursor: pointer;

  img {
    width: 100%;
    height: 100%;
  }
}

.progress-container {
  flex: 1;
  position: relative;
}

.progress-bar {
  height: 4px;
  background-color: #e1e1e1;
  border-radius: 2px;
  position: relative;
}

.progress-fill {
  position: absolute;
  height: 100%;
  background-color: #1890ff;
  border-radius: 2px;
  width: 40%;
}

.time-display {
  position: absolute;
  right: 0;
  top: -20px;
  font-size: 12px;
  color: #999;
}

.volume-control {
  margin-left: 15px;
  color: #999;
  cursor: pointer;
}

.quality-toggle {
  margin-left: 15px;
  font-size: 13px;
  color: #666;
  cursor: pointer;
  display: flex;
  align-items: center;
}

.quality-toggle i {
  margin-left: 4px;
  font-size: 12px;
}

.audio-info {
  text-align: center;
  font-size: 12px;
  color: #999;
}

.message {
  margin-bottom: 20px;
  position: relative;
}

.message-time {
  text-align: center;
  margin-bottom: 10px;
  font-size: 12px;
  color: #999;
}

.message-content {
  display: flex;
  max-width: 70%;
}

.message.left .message-content {
  float: left;
}

.message.right .message-content {
  float: right;
  flex-direction: row-reverse;
}

.avatar {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background-color: #ccc;
  overflow: hidden;
  margin: 0 10px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.avatar img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.avatar.robot {
  background-color: #e6f7ff;
  color: #1890ff;
  flex-shrink: 0;
}

.bubble {
  padding: 12px 16px;
  border-radius: 10px;
  position: relative;
  word-break: break-word;
  max-width: 100%;
}

.message.left .bubble {
  background-color: #f2f3f5;
  border-top-left-radius: 0;
}

.message.right .bubble {
  background-color: #e6f7ff;
  border-top-right-radius: 0;
  color: #333;
}

.message-indicator {
  padding: 4px 10px;
  border-radius: 12px;
  font-size: 11px;
  margin-top: 6px;
  display: inline-block;
}

.message.left .message-indicator {
  margin-left: 60px;
  background-color: #f9f9f9;
  color: #999;
}

.message.right .message-indicator {
  float: right;
  margin-right: 60px;
  background-color: #f0f7ff;
  color: #1890ff;
}

.message::after {
  content: "";
  display: table;
  clear: both;
}

.message-footer {
  margin-top: 5px;
  font-size: 12px;
  color: #999;
  display: flex;
  align-items: center;
}

.message.left .message-footer {
  margin-left: 60px;
}

.message.right .message-footer {
  margin-right: 60px;
  justify-content: flex-end;
}

.message-duration {
  margin-left: 10px;
}

.chat-footer {
  background-color: #fff;
  padding: 10px;
  display: flex;
  align-items: center;
  border-top: 1px solid #eaeaea;
}

.chat-footer-tools {
  display: flex;
}

.tool-button {
  width: 32px;
  height: 32px;
  display: flex;
  justify-content: center;
  align-items: center;
  color: #666;
  margin-right: 5px;
  border-radius: 4px;
  cursor: pointer;
}

.tool-button:hover {
  background-color: #f5f5f5;
}

.tool-button.send {
  background-color: #f5222d;
  color: white;
  margin-left: 5px;
}

.input-area {
  flex: 1;
  margin: 0 10px;
  border: 1px solid #eaeaea;
  border-radius: 4px;
  height: 36px;
  display: flex;
  align-items: center;
  padding: 0 10px;
}

/* 其他样式 */
.voice-icon {
  display: inline-block;
  width: 15px;
  height: 15px;
  background-color: #1890ff;
  border-radius: 50%;
  text-align: center;
  line-height: 15px;
  color: white;
  font-size: 10px;
  margin-right: 5px;
}

.clear-both {
  clear: both;
}
</style>
